﻿using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;

namespace personalplaner.common.controls
{
	public class CollapsibleItem : HeaderedContentControl
	{
		static CollapsibleItem()
        {
			DefaultStyleKeyProperty.OverrideMetadata(typeof(CollapsibleItem), new FrameworkPropertyMetadata(typeof(CollapsibleItem)));
        }

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
		}

        #region Implementation

        //protected override Size MeasureOverride(Size constraint)
        //{
        //    //UIElement child = Content as UIElement;
        //    //if (child != null)
        //    //{
        //    //    child.Measure(constraint);

        //    //    //double percent = AnimationProgress;
        //    //    //double width = CalculateWidth(child.DesiredSize.Width, percent, HorizontalReveal);
        //    //    //double height = CalculateHeight(child.DesiredSize.Height, percent, VerticalReveal);
        //    //    //return new Size(width, height);
        //    //return constraint;
        //    //}

        //    return new Size();
        //}

		private CollapsiblePanel ParentPanel
		{
			get
			{
				return ItemsControl.ItemsControlFromItemContainer(this) as CollapsiblePanel;
			}
		}

		#endregion

        #region Properties

        public bool IsCollapsed
		{
			get
			{
				return (bool)GetValue(IsCollapsedProperty);
			}
			set
			{
				SetValue(IsCollapsedProperty, value);
			}
		}

		public static readonly DependencyProperty IsCollapsedProperty =
			DependencyProperty.Register("IsCollapsed", typeof(bool), typeof(CollapsibleItem), new PropertyMetadata(false, new PropertyChangedCallback(OnIsExpandedChanged)));

		private static void OnIsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var ep = (CollapsibleItem)d;

			if (!(bool)e.NewValue)
			{
				ep.OnExpanded();
			}
			else
			{
				ep.OnCollapsed();
			}
		}

		/// <summary>
		/// Expanded event. 
		/// </summary>
		public static readonly RoutedEvent ExpandedEvent =
			EventManager.RegisterRoutedEvent("Expanded",
				RoutingStrategy.Bubble,
				typeof(RoutedEventHandler),
				typeof(CollapsibleItem)
			);

		/// <summary> 
		/// Expanded event. It is fired when IsExpanded changed from false to true.
		/// </summary>
		public event RoutedEventHandler Expanded
		{
			add { AddHandler(ExpandedEvent, value); }
			remove { RemoveHandler(ExpandedEvent, value); }
		}

		/// <summary> 
		/// Collapsed event.
		/// </summary>
		public static readonly RoutedEvent CollapsedEvent =
			EventManager.RegisterRoutedEvent("Collapsed",
				RoutingStrategy.Bubble,
				typeof(RoutedEventHandler),
				typeof(CollapsibleItem)
			);

		/// <summary>
		/// Collapsed event. It is fired when IsExpanded changed from true to false.
		/// </summary>
		public event RoutedEventHandler Collapsed
		{
			add { AddHandler(CollapsedEvent, value); }
			remove { RemoveHandler(CollapsedEvent, value); }
		}

		/// <summary>
		/// A virtual function that is called when the IsExpanded property is changed to true. 
		/// Default behavior is to raise an ExpandedEvent. 
		/// </summary>
		protected virtual void OnExpanded()
		{
			var args = new RoutedEventArgs
			{
				RoutedEvent = CollapsibleItem.ExpandedEvent,
				Source = this
			};
			RaiseEvent(args);
		}

		/// <summary>
		/// A virtual function that is called when the IsExpanded property is changed to false. 
		/// Default behavior is to raise a CollapsedEvent.
		/// </summary>
		protected virtual void OnCollapsed()
		{
			RaiseEvent(new RoutedEventArgs(CollapsibleItem.CollapsedEvent, this));
        }

        #endregion
    }

	public enum RevealDirection
	{
		Up,

		Down,

		Left,

		Right
	}
}
